package cn.iocoder.yudao.module.dbit.service.policynew;

import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.dbit.controller.admin.policynew.vo.*;
import cn.iocoder.yudao.module.dbit.dal.dataobject.policynew.PolicyNewDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.dbit.dal.mysql.policynew.PolicyNewMapper;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dbit.enums.ErrorCodeConstants.*;

/**
 * 政策新闻 Service 实现类
 *
 * @author 大奔
 */
@Service
@Validated
public class PolicyNewServiceImpl implements PolicyNewService {

    @Resource
    private PolicyNewMapper policyNewMapper;

    @Override
    public Integer createPolicyNew(PolicyNewSaveReqVO createReqVO) {
        // 插入
        PolicyNewDO policyNew = BeanUtils.toBean(createReqVO, PolicyNewDO.class);
        policyNewMapper.insert(policyNew);
        // 返回
        return policyNew.getId();
    }

    @Override
    public void updatePolicyNew(PolicyNewSaveReqVO updateReqVO) {
        // 校验存在
        validatePolicyNewExists(updateReqVO.getId());
        // 更新
        PolicyNewDO updateObj = BeanUtils.toBean(updateReqVO, PolicyNewDO.class);
        policyNewMapper.updateById(updateObj);
    }

    @Override
    public void deletePolicyNew(Integer id) {
        // 校验存在
        validatePolicyNewExists(id);
        // 删除
        policyNewMapper.deleteById(id);
    }

    private void validatePolicyNewExists(Integer id) {
        if (policyNewMapper.selectById(id) == null) {
            throw exception(POLICY_NEW_NOT_EXISTS);
        }
    }

    @Override
    @TenantIgnore
    public PolicyNewDO getPolicyNew(Integer id) {
        return policyNewMapper.selectById(id);
    }

    @Override
    @TenantIgnore
    public PageResult<PolicyNewDO> getPolicyNewPage(PolicyNewPageReqVO pageReqVO) {
        return policyNewMapper.selectPage(pageReqVO);
    }

    @Override
    @TenantIgnore
    public List<StudentPolicyNewReqVO> getLatestPolicyNew(Integer number) {
        return policyNewMapper.selectLatestPolicyNew(number);
    }

}